package com.zcx.qshop.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.view.AdaptListView;
import com.zcx.qshop.R;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.conn.JsonOrderAsyGet;
import com.zcx.qshop.entity.Good;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/8/22 0022.
 */
public abstract class MyOrderAdapter extends QSAdapter<JsonOrderAsyGet.Info.Order> {

    private LayoutInflater layoutInflater;

    public MyOrderAdapter(Context context, List<JsonOrderAsyGet.Info.Order> objects) {

        super(context, objects);

        layoutInflater = ((Activity)context).getLayoutInflater();

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final JsonOrderAsyGet.Info.Order order = getItem(position);

        ViewHolder viewHolder;

        if(convertView == null){

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), QSApplication.ScaleScreenHelper.loadView((ViewGroup) layoutInflater.inflate(R.layout.item_my_order, null)));

            viewHolder.adaptListView.setAdapter(viewHolder.goodAdapter = new GoodAdapter(getContext(), viewHolder.goods));

            convertView.setTag(viewHolder);

        }else{

            viewHolder = (ViewHolder) convertView.getTag();

        }

        viewHolder.goods.clear();

        viewHolder.goods.addAll(order.goods);

        viewHolder.goodAdapter.notifyDataSetChanged();

        viewHolder.time.setText(order.posttime);

        viewHolder.state.setText(order.type);

        viewHolder.type.setText("共" + order.count + "件商品");

        viewHolder.tolat.setText(order.account);

        viewHolder.adaptListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                onDetail(order);

            }

        });

        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                onDetail(order);

            }

        });

        setState(viewHolder, order.checkinfo, new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                switch (v.getId()) {

                    case R.id.item_my_order_refund:

                        onRefund(order);

                        break;

                    case R.id.item_my_order_receipt:

                        onReceipt(order);

                        break;

                    case R.id.item_my_order_delete:

                        onDelete(order);

                        break;

                    case R.id.item_my_order_assess:

                        onAssess(order);

                        break;

                    case R.id.item_my_order_pay:

                        onPay(order);

                        break;

                }

            }

        });

        return convertView;

    }

    private void setState(ViewHolder viewHolder, String checkinfo, View.OnClickListener onClickListener){

        viewHolder.refund.setVisibility(View.GONE);

        viewHolder.receipt.setVisibility(View.GONE);

        viewHolder.delete.setVisibility(View.GONE);

        viewHolder.assess.setVisibility(View.GONE);

        viewHolder.pay.setVisibility(View.GONE);

        if(checkinfo.equals("0") || checkinfo.equals("-10")){

            viewHolder.delete.setVisibility(View.VISIBLE);

            viewHolder.delete.setOnClickListener(onClickListener);

        }else if(checkinfo.equals("1")){

            viewHolder.pay.setVisibility(View.VISIBLE);

            viewHolder.pay.setOnClickListener(onClickListener);

            viewHolder.delete.setVisibility(View.VISIBLE);

            viewHolder.delete.setOnClickListener(onClickListener);

        }else if(checkinfo.equals("2")){

            viewHolder.receipt.setVisibility(View.VISIBLE);

            viewHolder.receipt.setOnClickListener(onClickListener);

        }else if(checkinfo.equals("3")){

            viewHolder.receipt.setVisibility(View.VISIBLE);

            viewHolder.receipt.setOnClickListener(onClickListener);

        }else if(checkinfo.equals("10")){

            viewHolder.refund.setVisibility(View.VISIBLE);

            viewHolder.refund.setOnClickListener(onClickListener);

            viewHolder.delete.setVisibility(View.VISIBLE);

            viewHolder.delete.setOnClickListener(onClickListener);

            viewHolder.assess.setVisibility(View.VISIBLE);

            viewHolder.assess.setOnClickListener(onClickListener);

        }else if(checkinfo.equals("15")){

            viewHolder.delete.setVisibility(View.VISIBLE);

            viewHolder.delete.setOnClickListener(onClickListener);

        }

    }

    protected abstract void onDetail(JsonOrderAsyGet.Info.Order order);

    protected abstract void onRefund(JsonOrderAsyGet.Info.Order order);

    protected abstract void onReceipt(JsonOrderAsyGet.Info.Order order);

    protected abstract void onDelete(JsonOrderAsyGet.Info.Order order);

    protected abstract void onAssess(JsonOrderAsyGet.Info.Order order);

    protected abstract void onPay(JsonOrderAsyGet.Info.Order order);

    private class ViewHolder{

        @BoundView(R.id.item_my_order_time) public TextView time;

        @BoundView(R.id.item_my_order_state) public TextView state;

        @BoundView(R.id.item_my_order_type) public TextView type;

        @BoundView(R.id.item_my_order_tolat) public TextView tolat;

        @BoundView(R.id.item_my_order_list_view) public AdaptListView adaptListView;

        @BoundView(R.id.item_my_order_refund) public View refund;

        @BoundView(R.id.item_my_order_receipt) public View receipt;

        @BoundView(R.id.item_my_order_delete) public View delete;

        @BoundView(R.id.item_my_order_assess) public View assess;

        @BoundView(R.id.item_my_order_pay) public View pay;

        public List<Good> goods = new ArrayList<>();

        public GoodAdapter goodAdapter;

    }

}